package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.ModelDeploymentContext;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.ModelDeploymentPostProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowReTimeoutSettingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Processor - 解析模型配置的超时、预警
 *
 * @author lilh
 * @date 2021/4/26 14:28
 */
@Component
public class SaveModelTimeoutNoticeProcessor implements ModelDeploymentPostProcessor {

    @Autowired
    private FlowReTimeoutSettingRepository flowReTimeoutSettingRepository;

    @Override
    public void process(ModelDeploymentContext context) {
        saveProcessTimeoutNotice(context.getDeployId(), context.getModelJson());
    }

    private void saveProcessTimeoutNotice(String deployId, JsonNode modelJson) {
        //        JsonNode nodes = elementNode.get("childShapes");
        Map<String, JsonNode> taskUsers = new HashMap<>();
        //        if (!nodes.isNull()) {
        //            for (int i = 0; i < nodes.size(); i++) {
        //                JsonNode node = nodes.get(i);
        //                if("UserTask".equals(node.get("stencil").get("id").textValue())){
        //                    JsonNode properties = node.get("properties");
        //                    JsonNode candidateUsers = properties.get("candidateUsers");
        //                    taskUsers.put(properties.get("overrideid").textValue(),candidateUsers);
        //                }
        //            }
        //        }
        JsonNode properties = modelJson.get("properties");
        if (!properties.isNull()) {
            JsonNode timeoutSetting = properties.get("timeoutSetting");
            if (!timeoutSetting.isNull()) {
                flowReTimeoutSettingRepository.saveTimeoutNoticeSetting(deployId, null, timeoutSetting, taskUsers);
            }
        }
    }
}
